Document select_related limitations and provide comprehensive workarounds #95
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses the question about
select_related
support by adding comprehensive documentation explaining why it's not supported and providing practical alternatives.Background
The issue asked whether
select_related
would be considered out of scope for django-tree-queries. As discussed in the comments,select_related
support is intentionally not implemented due to the complexity of combining recursive Common Table Expressions (CTEs) with Django's join mechanisms, which could compromise the library's stability across Django versions.Changes
Added a new "Working with Related Models and Constraints" section to the README that includes:
Clear Explanation of Limitations
select_related()
is not supported with tree queriesComprehensive Workarounds with Examples
Using
prefetch_related()
instead:Using Django's
Prefetch()
for fine control:Tree queries in filter conditions (proven pattern from test suite):
Design Philosophy Context
Explains why these constraints exist and how they maintain django-tree-queries' core strengths:
Validation
All documented patterns are validated against existing test cases, particularly the
test_reference()
method which extensively uses the recommendedtree_field__in=tree_node.descendants()
pattern. The examples follow established Django ORM best practices and provide clear, maintainable alternatives toselect_related()
.This documentation-first approach respects the maintainer's decision about scope while empowering developers with proven techniques for working efficiently with both tree models and related models.
Fixes #18.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.